package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class ServerInfoDao {
	
	@Inject
	private Dao gisplatform;
 	
    public Boolean execuleSQL(String updateSql ){
    	
    	Boolean result = false;
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
  
	/**
	 * 查询用户权限
	 * @param selectSql
	 * @return
	 */
	public String queryData(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				int rows = 0;
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
			    	 rows++;
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
			     JSONObject ret=new JSONObject();
			     ret.put("total", rows);
			     result.put(ret);
				return result.toString();
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	public String getControlRegion(String roleName){
		String result = "";
		if(!StringUtil.isNullOrEmpty(roleName)){
			 
			
		}
		return result;
	}


	public List<ServerInfo> findServer(String selectstr) {
		Sql sql = Sqls.create(selectstr);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<ServerInfo> result = new ArrayList<ServerInfo>();
			     while (rs.next()){
			    	 ServerInfo roleInfo=new ServerInfo();
			    	 if(rs.findColumn("ID")>0) roleInfo.setId(rs.getString("ID"));
			    	 if(rs.findColumn("SERVER_NAME")>0) roleInfo.setServerName(rs.getString("SERVER_NAME"));
			    	 if(rs.findColumn("TYPE")>0) roleInfo.setType(rs.getString("TYPE"));
			    	 if(rs.findColumn("DYNAMICS_URL")>0) roleInfo.setDynamicsUrl(rs.getString("DYNAMICS_URL"));
			    	 if(rs.findColumn("FEATURE_URL")>0) roleInfo.setFeatureUrl(rs.getString("FEATURE_URL"));
			    	 if(rs.findColumn("STATUS")>0) roleInfo.setStatus(rs.getString("STATUS"));
			    	 result.add(roleInfo);
			     }
			     
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(List.class);
	    
	}


	public String queryBaseServerInfoAll(int from, int to, String name) {
		Cnd cnd = null;
		if( StringUtil.isNullOrEmpty(name)){
			name="";		}
		cnd = Cnd.where("SERVER_NAME","LIKE","%"+name+"%");
		cnd.desc("CREATE_TIME");
		Pager pager = new Pager();
		pager.setPageNumber(from);
		pager.setPageSize(to);
		pager.setRecordCount(to);
		List<ServerInfo> list = gisplatform.query(ServerInfo.class, cnd, pager);
		if(list!=null && list.size()>0){
			JSONArray result=new JSONArray();
		    for(ServerInfo role : list){
		    	JSONObject ret=new JSONObject();
		    	ret.put("id", role.getId());
		    	ret.put("name", role.getServerName());
		    	ret.put("dynamicsUrl", role.getDynamicsUrl());
		    	ret.put("featureUrl", role.getFeatureUrl());
		    	ret.put("type", role.getType());
		    	ret.put("status", role.getStatus());
		    	ret.put("createTime", role.getCreateTime());
		    	result.put(ret);
		    }
		    return result.toString();
		    	 
		}
		
		return null;
	}
	
	public int queryBaseServerInfoCount( String roleName) {
		Cnd cnd = null;;
		if(!StringUtil.isNullOrEmpty(roleName)){
			cnd = Cnd.where("SERVER_NAME","LIKE","%"+roleName+"%");
		}
		List<ServerInfo> listSec =gisplatform.query(ServerInfo.class, cnd, null);
		
		if(listSec!=null && listSec.size()>0){
			return listSec.size();
		}else{
			return 0;
		}
	}




	public boolean addServerInfo(ServerInfo serverInfo) {
		ServerInfo t = gisplatform.insert(serverInfo);
		if(t!=null){
			return true;
		}
		return false;
	}

    public void why(String how){
    	System.out.println("");
    }


	public boolean updateServerInfo(ServerInfo serverInfo) {
		if(gisplatform.updateIgnoreNull(serverInfo)>0)
			return true;
		return false;
	}



	public boolean delServerInfo(ServerInfo serverInfo) {
		
		int res = gisplatform.delete(serverInfo);
		if(res>0){
			return true;
		}
		return false;
	}




	public ServerInfo findServerInfoById(String id) {
		ServerInfo role = new ServerInfo();
		role.setId(id);
		ServerInfo res = gisplatform.fetch(ServerInfo.class,id );
		return res;
	}
	
}
